﻿using RLAWEB.Models;
using RLAWEB.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace RLAWEB
{
    public partial class SearchFriends : Page
    {
        private RealLifeServiceClient _service;
        private User _loginUser;
        private List<User> _friends;
        private List<Friend> _pendingFriends;
        private List<User> _users;

        protected void Page_Load(object sender, EventArgs e)
        {
            _service = new RealLifeServiceClient();

            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                _loginUser = Helper.GetLoggedInUser(authCookie);
            }
        }

        protected void Search(string[] searchTerms)
        {
            if (_service == null)
                _service = new RealLifeServiceClient();
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                // _loginUser = Helper.GetLoggedInUser(authCookie);
                BindSearchList(searchTerms);
            }
        }

        private void BindSearchList(string[] searchTerms)
        {
            string[] search = searchTerms;
            _users = _service.SearchUserList(search.ToList());
            searchList.DataSource = _users;

            //Set the list of friends and pending friendships
            _friends = _service.GetFriendships(_loginUser);
            _pendingFriends = _service.GetPendingFriendships(_loginUser);
            List<int> pfIds = _pendingFriends.Select(x => x.Requested.UserId).ToList();
            List<int> fIds = _friends.Select(x => x.UserId).ToList();

            //Remove these ids from the user list
            _users.RemoveAll(x => pfIds.Contains(x.UserId));
            _users.RemoveAll(x => fIds.Contains(x.UserId));

            //Bind the repeater to the user list
            searchList.DataBind();
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            string searchTerms = textInput.Text;
            string[] keywords = searchTerms.Split(new char[0]);
            Search(keywords);
        }

        protected void RequestFriendship_Click(object sender, EventArgs e)
        {
            User requested = _service.GetUserById(Convert.ToInt32(Request.QueryString["ID"]));
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                _loginUser = Helper.GetLoggedInUser(authCookie);
                //lblTest.Text = _loginUser.UserId + " + " + requested.UserId;
                int result = _service.RequestFriendship(_loginUser, requested);

                string searchTerms = textInput.Text;
                string[] keywords = searchTerms.Split(new char[0]);
                BindSearchList(keywords);
            }
        }
    }
}